﻿namespace Algorithm
{
    using System;

    public class Tile : ITile
    {
        public Tile(Point2D centralPoint, string[] pointsDirections, int movePriority)
        {
            this.CentralPoint = centralPoint;
            this.PointsDirections = pointsDirections;
            this.MovePriority = movePriority;

            if (movePriority >= PriorityLevels)
            {
                PriorityLevels = movePriority + 1;
            }
        }

        public static int PriorityLevels { get; private set; }

        public Point2D CentralPoint { get; set; }

        public string[] PointsDirections { get; private set; }

        public int MovePriority { get; private set; }

        public Point2D[] GetAllPoints(Point2D centralPoint = null)
        {
            if (centralPoint == null)
            {
                centralPoint = this.CentralPoint;
            }

            Point2D[] points = new Point2D[this.PointsDirections.Length + 1];
            points[0] = centralPoint;

            for (int i = 1; i <= this.PointsDirections.Length; i++)
            {
                string currentDirection = this.PointsDirections[i - 1];
                switch (currentDirection)
                {
                    case "u":
                        points[i] = new Point2D(centralPoint.Row - 1, centralPoint.Col);
                        break;
                    case "d":
                        points[i] = new Point2D(centralPoint.Row + 1, centralPoint.Col);
                        break;
                    case "l":
                        points[i] = new Point2D(centralPoint.Row, centralPoint.Col - 1);
                        break;
                    case "r":
                        points[i] = new Point2D(centralPoint.Row, centralPoint.Col + 1);
                        break;
                    case "ul":
                        points[i] = new Point2D(centralPoint.Row - 1, centralPoint.Col - 1);
                        break;
                    case "ur":
                        points[i] = new Point2D(centralPoint.Row - 1, centralPoint.Col + 1);
                        break;
                    case "dl":
                        points[i] = new Point2D(centralPoint.Row + 1, centralPoint.Col - 1);
                        break;
                    case "dr":
                        points[i] = new Point2D(centralPoint.Row + 1, centralPoint.Col + 1);
                        break;
                    default:
                        throw new ArgumentException(string.Format("Invalid direction {0}", currentDirection));
                }
            }

            return points;
        }
    }
}